<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        #box{
            padding: 5px;
            width: 500px;
            height: 300px;
            background-color: sandybrown;
           display: none;
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%,-50%) 
          
        }
        #btn{
            
            display: block;
            margin: 0 auto;
        }
        span{
            width: 100%;
            height: 30px;
            display: inline-block;
            background-color: seagreen;
            line-height: 30px;
            padding: 4px;
            box-sizing: border-box;
        }
        .btn2{
            float: right;
            border: 1px solid red;
            color: red;
        }
        .box2{
            width: 100%;
            margin-top: 20px;
            height: 80%;
            background-color: #fff;

        }
    </style>
</head>
<body>
   <center><button id="btn">弹出层</button></center> 
  <div id="box">
    <span><button id="btn2" class="btn2">X</button></span>
    <div class="box2"></div>
</div>
    
  <script>
     window.onload = function (){
    var btn = document.getElementsByTagName('button')[0];
      var box = document.getElementById('box');
      var btn2 = document.getElementById('btn2');
      btn.onclick = function (){
        box.style.display = 'block';
        document.body.style.background = '#ccc'
      }
      btn2.onclick = function (){
        box.style.display = 'none';
        document.body.style.background = '#fff'
      }
         
    
     }


  </script>
</body>
</html>